import type { ResponseData, ResponsePaginationData } from '@/utils/request';
import { SUCCESS_CODE } from '@/utils/request';
import type { RequestData } from '@ant-design/pro-table';
import type {
  Tenant,
  TenantAddItem,
  TenantDetailItem,
  TenantEditItem,
  TenantPageQueryListParams,
  TenantTableListItem,
} from './data.d';
import { request } from 'umi';
import { message } from 'antd';
import type { TenantItem } from '@/pages/user/User/data';

export { queryTenantRoleList } from '../TenantRole/service';

/**
 * 获取客户列表
 * @param params
 */
export async function queryTenantList(
  params: TenantPageQueryListParams,
): Promise<RequestData<TenantTableListItem>> {
  const { current: page, pageSize = 20, name } = params;
  try {
    const res: ResponsePaginationData<Tenant> = await request('/tenant/list', {
      params: { ...params, page, pageSize, name },
    });
    if (res?.code !== SUCCESS_CODE) {
      return { success: false, total: 0, data: [] };
    }
    return {
      success: true,
      total: res.data.total,
      data: res.data.list,
    };
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return { code: 1, data: [] };
  }
}

export async function queryTenantDetail(params: {
  id: string;
}): Promise<{ success: boolean; data?: TenantDetailItem }> {
  try {
    const res = await request(`/tenant/${params.id}`);

    if (res?.code !== SUCCESS_CODE) {
      return { success: false };
    }

    return {
      success: true,
      data: res.data,
    };
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return { success: true, data: undefined };
  }
}

/**
 * 创建一个客户
 * @param params
 */
export async function addTenant(params: TenantAddItem) {
  const { name, topUserName, password, dataBoardUrl } = params;
  try {
    const res = await request('/tenant/create', {
      method: 'POST',
      data: { name, account: topUserName, password, dataBoardUrl },
    });
    if (!res || res.code !== SUCCESS_CODE) {
      message.error('获取失败');
      return { success: false, total: 0, data: [] };
    }
    return res;
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return { code: 1, data: [] };
  }
}

export async function editTenant(params: TenantEditItem) {
  const { name, dataBoardUrl } = params;
  try {
    const res = await request(`/tenant/update`, {
      method: 'POST',
      data: { tenantId: params.id, name, dataBoardUrl },
    });
    return res;
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return { code: 1, data: [] };
  }
}

// export async function editTenantRoles(params: TenantEditItem) {
//   console.log('params', params);

//   // const roleIds = roles?.map((item) => item.id);
//   try {
//     const res = await request(`/tenant/update`, {
//       method: 'POST',
//       data: { tenantId: parseInt(params.id), name: params.name },
//     });
//     return res;
//   } catch (error) {
//     message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
//     return { code: 1, data: [] };
//   }
// }

export async function removeTenant(params: { id: string }): Promise<void> {
  const res: ResponseData<any> = await request(`/tenant/${params.id}`, { method: 'DELETE' });

  if (res.code !== SUCCESS_CODE) {
    throw new Error(res.message);
  }
}

export async function getTenantOptions(params: { name?: string }) {
  const res: ResponsePaginationData<TenantItem> = await request('/tenant/get_tenant_options', {
    ...params,
    params: { name: params.name },
  });
  if (res?.code !== SUCCESS_CODE) {
    return { success: false, total: 0, data: [] };
  }

  return {
    success: true,
    data: res.data.list,
  };
}
